﻿/**
  ******************************************************************************
  * Copyright © Beijing WIIP Co., Ltd. 2022-2024. All rights reserved.
  ******************************************************************************
  * @file: disp_func.c
  * @brief: 功能设置界面
  *
  ******************************************************************************
  * @note:
  *
  ******************************************************************************
  * Change Logs:
  * Date           Author       Notes
  * 2024-07-25     MLB          the first version
  * 2024-10-09     MLB          the second version
  *
  ******************************************************************************
  */

/* 声明包含头文件 --------------------------------------------------------------*/

#include "../Inc/data_public.h"
#include "../Inc/disp_func.h"
#include <stdlib.h>
#include <stdint.h>

/* 声明私有类型定义--------------------------------------------------------------*/
typedef struct Label_t
{
	char* label_cont;				// 标签内容
	lv_coord_t width;				// 标签宽度
	lv_coord_t height;				// 标签高度
	lv_coord_t pos_x;				// 标签x坐标
	lv_coord_t pos_y;				// 标签y坐标
}Labet_t;
typedef struct Btn_t
{
	uint8_t btn_id;					// 按钮ID
	char* btn_cont;					// 按钮内容
	lv_style_t * btn_style;			// 按钮风格
	lv_coord_t pos_x;				// 按钮x坐标
	lv_coord_t pos_y;				// 按钮y坐标
	void * data;					// 控制按钮标志位
}Btn_t;

typedef struct listBtnStr{
	uint16_t isEnable;/* 校准是否使能 */
	char* taitle;     /* 显示的标题 */
	void (*tab_create_func)(void);
}ListBtn_t;
//typedef struct tableRecort_t{
//	float program_SP;		        // 程序目标温度
//	uint16_t program_StableTime;    //程序目标维持时长
//	uint16_t program_Step;          //程序当前序号
//	uint16_t program_Time_ThisStep; //程序当前时间设置
//}tableRecort_t;
//static tableRecort_t taUser_table[8];		 // 实际数值
/* 声明私有变量 ---------------------------------------------------------------*/
static kb_userData_t userData[2];
static ListBtn_t listBtn[3];				 // 侧边栏按钮
static Btn_t   taUser_btn[7];				 // 按钮
static Labet_t taUser_label[9];              // 标签
static ta_userData_t taUser_textarea[2];	 // 输入框
//static tableRecort_t taUser_table[8];		 // 实际数值
static lv_obj_t* outLable;                   // 实时显示排液时长
static lv_obj_t* blowLable;                  // 实时显示吹干时长
static lv_obj_t* list_ctrlTempInfo;			 // 程序控温界面
static lv_obj_t* list_titleInfo;
static lv_obj_t* lv_msgbox;
static lv_obj_t* checkBox_loopEnable;	     // 循环使能
static lv_obj_t* btn_ctrlTempStart;			 // 程序控温启动按钮
static uint8_t now_tab_ID;					 /* 现在的tab ID */
static lv_obj_t* tabCont;
static lv_timer_t* timer_refresh_func;
static uint8_t ctrl_lastRow = 255;
static uint8_t ctrl_lastCol = 255;
//static tableRecort_t last_record[8];
/* 声明私有常量 ---------------------------------------------------------------*/

/* 声明私有宏定义 --------------------------------------------------------------*/
#define LIST_BTN_QTY (2)	/* 边栏按键数量 */
LV_IMG_DECLARE(icon1);
/* 声明私有函数 ---------------------------------------------------------------*/

/**
  * @brief	刷新所有按钮状态
  * @param  parent:父对象
  * @retval None
  */
static void btn_refresh_by(lv_obj_t * parent)
{
	/* 遍历所有的文本框，对文本框的内容进行更新 */
	for(uint8_t i = 0; i < lv_obj_get_child_cnt(parent); i++)
	{
		lv_obj_t* child = lv_obj_get_child(parent, i);
		if(lv_obj_check_type(child, &lv_btn_class))
		{
			Btn_t btnInfo = *(Btn_t *)lv_obj_get_user_data(child);
			if (btnInfo.btn_id % 2 == 0)
			{
				if (*(uint16_t*)btnInfo.data == 0)
				{
					lv_obj_add_state(child, LV_STATE_CHECKED);
				}
				else
				{
					lv_obj_clear_state(child, LV_STATE_CHECKED);
				}
			}
			else
			{
				if (*(uint16_t*)btnInfo.data == 1)
				{
					lv_obj_add_state(child, LV_STATE_CHECKED);
				}
				else
				{
					lv_obj_clear_state(child, LV_STATE_CHECKED);
				}
			}

		}
	}
}
static void btn_event_cb(lv_event_t * e)
{
	lv_obj_t * obj = lv_event_get_target(e);		// 获取触发源
	lv_event_code_t code = lv_event_get_code(e);	// 获取触发条件
	Btn_t btnInfo = *(Btn_t *)lv_obj_get_user_data(obj);
	if (code == LV_EVENT_CLICKED)
	{
		if (btnInfo.btn_id != 4 && btnInfo.btn_id != 5)  // 数据存储到主板
		{
			if (btnInfo.btn_id % 2 == 0)
			{
				if (*(uint16_t*)btnInfo.data == 1)
				{
					*(uint16_t*)btnInfo.data = 0;
				}
			}
			else
			{
				if (*(uint16_t*)btnInfo.data == 0)
				{
					*(uint16_t*)btnInfo.data = 1;
				}

			}
			send_aux((uint16_t*)btnInfo.data);
		}
		else
		{
			if (btnInfo.btn_id == 4)			// 数据存储到屏幕
			{
				if (*(uint8_t*)btnInfo.data == 1)   //todo:后面能够将标志位类型统一
				{
					*(uint8_t*)btnInfo.data = 0;
				}
			}
			else if (btnInfo.btn_id == 5)
			{
				if (*(uint8_t*)btnInfo.data == 0)
				{
					*(uint8_t*)btnInfo.data = 1;
				}
			}
	        /* 将屏幕参数保存到屏幕中去 */
	        if (g_RW_State == RW_READY)
	        {
	            g_RW_State = RW_WRITE_NAND_SCREEN;
	        }
		}
		/* 刷新按钮状态 */
		btn_refresh_by(tabCont);
	}
}
/**
  * @brief	初始化所有按钮
  * @param  None
  * @retval None
  */
static void btn_init(void)
{
	/* 按钮标题 */
	taUser_btn[0].btn_cont = dispText[DA.typeLanguage].manual;
	taUser_btn[0].data = &UP.Type_Start;
	taUser_btn[1].btn_cont = dispText[DA.typeLanguage].Auto;
	taUser_btn[1].data = &UP.Type_Start;
	taUser_btn[2].btn_cont = dispText[DA.typeLanguage].flowCtrl;
	taUser_btn[2].data = &UP.Type_FlowContr;
	taUser_btn[3].btn_cont = dispText[DA.typeLanguage].pressCtrl;
	taUser_btn[3].data = &UP.Type_FlowContr;
	taUser_btn[4].btn_cont = dispText[DA.typeLanguage].off;
	taUser_btn[4].data = &DA.lockModeFlag;
	taUser_btn[5].btn_cont = dispText[DA.typeLanguage].on;
	taUser_btn[5].data = &DA.lockModeFlag;
//	taUser_btn[6].btn_cont = dispText[DA.typeLanguage].start;
//	taUser_btn[6].data = &UP.State_Return;
	for (uint8_t i = 0; i < 6; i++)
	{
		taUser_btn[i].btn_style = &style_btn4;
		taUser_btn[i].btn_id = i;
		if (i % 2 == 0)
		{
			taUser_btn[i].pos_x = lv_pct(38);
			taUser_btn[i].pos_y = lv_pct(13 * (i) + 8);

			taUser_btn[i + 1].pos_x = lv_pct(68);
			taUser_btn[i + 1].pos_y = lv_pct(13 * (i) + 8);
		}

	}
//	taUser_btn[6].pos_x = lv_pct(60);
//	taUser_btn[6].pos_y = lv_pct(70);
}
/**
  * @brief	创建按钮
  * @param  cont：按钮父对象  btnInfo:按钮信息结构体
  * @retval None
  */
static void btn_create_by(lv_obj_t * cont, Btn_t * btnInfo)
{
	lv_obj_t * newBtn= lv_btn_create(cont);
    lv_obj_add_style(newBtn, btnInfo->btn_style, LV_STATE_DEFAULT);
    lv_obj_align(newBtn,LV_ALIGN_TOP_LEFT, btnInfo->pos_x, btnInfo->pos_y);
    lv_obj_t * lable = lv_label_create(newBtn);
    lv_label_set_text(lable, btnInfo->btn_cont);
    lv_obj_align_to(lable, newBtn, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_style_bg_color(newBtn, lv_color_hex(0x3a96f1), LV_STATE_CHECKED);
    lv_obj_set_user_data(newBtn, btnInfo);
    lv_obj_add_event_cb(newBtn, btn_event_cb, LV_EVENT_CLICKED, btnInfo);
}

/**
  * @brief	初始化所有显示标签
  * @param  None
  * @retval None
  */
static void label_init(void)
{
	taUser_label[0].label_cont = dispText[DA.typeLanguage].startMode;
	taUser_label[1].label_cont = dispText[DA.typeLanguage].flowMode;
	taUser_label[2].label_cont = dispText[DA.typeLanguage].lock;
	taUser_label[3].label_cont = dispText[DA.typeLanguage].drainTime;
	taUser_label[4].label_cont = dispText[DA.typeLanguage].dryingTime;

	for (uint8_t i = 0; i < 5; i++)
	{
		taUser_label[i].width = lv_pct(35);
		taUser_label[i].height = lv_pct(15);
		taUser_label[i].pos_x = lv_pct(3);
		if (i < 3)
		{
			taUser_label[i].pos_y = lv_pct(25 * (i) + 10);		// 选项卡一显示标签位置
		}
		else
		{
			taUser_label[i].pos_y = lv_pct(30 * (i - 3) + 10);	// 选项卡二显示标签位置
		}
	}
	for (uint8_t i = 0; i <= 2; i += 2)
	{
		taUser_label[5 + i].height = lv_pct(15);
		taUser_label[5 + i].width = lv_pct(5);
		taUser_label[5 + i].label_cont = "/";
		taUser_label[5 + i].pos_x = lv_pct(48);
		taUser_label[5 + i].pos_y = lv_pct(15 * i + 10);

		taUser_label[6 + i].height = lv_pct(15);
		taUser_label[6 + i].width = lv_pct(5);
		taUser_label[6 + i].label_cont = "S";
		taUser_label[6 + i].pos_x = lv_pct(78);
		taUser_label[6 + i].pos_y = lv_pct(15 * i + 10);
	}

}
/**
  * @brief	创建标签
  * @param  cont: 标签父对象  labInfo:标签信息结构体
  * @retval None
  */
static void label_create_by(lv_obj_t* cont, Labet_t * labInfo)
{
	lv_obj_t* label = lv_label_create(cont);
	lv_label_set_text(label, labInfo->label_cont);
    lv_obj_set_style_width(label, labInfo->width, LV_STATE_DEFAULT);
    lv_obj_set_style_height(label, labInfo->height, LV_STATE_DEFAULT);
    lv_obj_set_style_text_color(label, lv_color_hex(0xffffff), LV_STATE_DEFAULT);
    lv_obj_align(label, LV_ALIGN_TOP_LEFT, labInfo->pos_x, labInfo->pos_y);  // 位置
}
/**
  * @brief	输入框初始化函数
  * @param  None
  * @retval None
  */
static void textarea_init(void)
{
	taUser_textarea[0].data.userData.value = &UP.Time_RetunFluid;
	taUser_textarea[0].data.userData.store_enable = 1;
	taUser_textarea[1].data.userData.value = &UP.Time_RetunAir;
	taUser_textarea[1].data.userData.store_enable = 1;
	for (uint8_t i = 0; i < 2; i++)
	{
		taUser_textarea[i].data.min = 0;
		taUser_textarea[i].data.max = 10000;
		taUser_textarea[i].width = lv_pct(20);
		taUser_textarea[i].height = lv_pct(10);
		taUser_textarea[i].max_length = 6;
		taUser_textarea[i].accepted_chars = "0123456789-.";
		taUser_textarea[i].one_line = true;
		taUser_textarea[i].pos_x = lv_pct(55);
		taUser_textarea[i].type = "%d";
	}
	taUser_textarea[0].pos_y = lv_pct(6);
	taUser_textarea[1].pos_y = lv_pct(36);
}
/**
  * @brief	定时刷新数据回调函数
  * @param  None
  * @retval None
  */
static void refresh_func_cb(lv_timer_t* timer)
{
	LV_UNUSED(timer);
	/* 遍历屏幕的父对象的所有子对象 */
	for(uint8_t i = 0; i < lv_obj_get_child_cnt(tabCont); i++)
	{
		lv_obj_t* child = lv_obj_get_child(tabCont, i);
		/* 遍历所有输入框子对象 */
		if(lv_obj_check_type(child, &lv_textarea_class))
		{
			char str[10] = {0};
			ta_userData_t userData = *(ta_userData_t *)lv_obj_get_user_data(child);
			if(type_is_float(userData.type))
				sprintf(str, userData.type, *(float*)userData.data.userData.value);
			else
				sprintf(str, userData.type, *(uint16_t*)userData.data.userData.value);
			lv_textarea_set_text(child, str);
		}

	}
//	if (funcTabView == 1)
//	{
//		lv_label_set_text_fmt(outLable, "%d", UP.gRunTime_RetunFluid);
//		lv_label_set_text_fmt(blowLable, "%d", UP.gRunTime_RetunAir);
//	}
	if (now_tab_ID == 1)
	{
		for (uint8_t i = 0; i <8; i++)
		{
			lv_table_set_cell_value_fmt(list_ctrlTempInfo, i, 0, "%d", DA.taUser_table[i].program_Step);
			lv_table_set_cell_value_fmt(list_ctrlTempInfo, i, 1, "%.1f",DA.taUser_table[i].program_SP);
			lv_table_set_cell_value_fmt(list_ctrlTempInfo, i, 2, "%d", DA.taUser_table[i].program_Time_ThisStep);
		}
    	if (UP.ES_ProTempCtrl == 0)
    	{
    		lv_obj_clear_state(btn_ctrlTempStart, LV_STATE_CHECKED);
    	}
    	else
    	{
    		lv_obj_add_state(btn_ctrlTempStart, LV_STATE_CHECKED);
    	}
	}
}

/**
 * @brief  选项卡一创建函数
 * @param  无
 * @return 无
 */
static void modeView_create(void)
{
    /**********************   选项卡一  ***********************/
    /* 创建屏幕显示标签 */
    for (uint8_t i = 0; i < 3; i++)
    {
    	label_create_by(tabCont, &taUser_label[i]);
    }

    /* 创建屏幕按钮 */
    for (uint8_t i = 0; i < 6; i++)
    {
    	btn_create_by(tabCont, &taUser_btn[i]);
    }
    /* 刷新按钮状态 */
    btn_refresh_by(tabCont);

}
/**
 * @brief  自定义选项卡二创建函数
 * @param  无
 * @return 无
 */
static void blowView_create(void)
{
    /**********************   选项卡二  ***********************/

    /* 创建静态标签 */
    for (uint8_t i = 3; i < 9; i++)
    {
    	label_create_by(tabCont, &taUser_label[i]);
    }
    /* 创建动态标签 */
    outLable = lv_label_create(tabCont);
    lv_obj_set_style_width(outLable, lv_pct(15), LV_STATE_DEFAULT);
    lv_obj_set_style_height(outLable, lv_pct(15), LV_STATE_DEFAULT);
    lv_obj_set_style_text_color(outLable, lv_color_hex(0xffffff), LV_STATE_DEFAULT);
    lv_label_set_text_fmt(outLable, "%d", UP.gRunTime_RetunFluid);
    lv_obj_align(outLable, LV_ALIGN_TOP_LEFT, lv_pct(40), lv_pct(10));  // 位置

    blowLable = lv_label_create(tabCont);
    lv_obj_set_style_width(blowLable, lv_pct(15), LV_STATE_DEFAULT);
    lv_obj_set_style_height(blowLable, lv_pct(15), LV_STATE_DEFAULT);
    lv_obj_set_style_text_color(blowLable, lv_color_hex(0xffffff), LV_STATE_DEFAULT);
    lv_label_set_text_fmt(blowLable, "%d", UP.gRunTime_RetunAir);
    lv_obj_align(blowLable, LV_ALIGN_TOP_LEFT, lv_pct(40), lv_pct(40));  // 位置
    /*创建输入框 */
	for(uint8_t i = 0; i < 2; i++)
	{
		textarea_create_by(tabCont, &taUser_textarea[i]);
	}
	/* 创建按钮 */
	btn_create_by(tabCont, &taUser_btn[6]);
    /* 刷新按钮状态 */
    btn_refresh_by(tabCont);

}
static void msgbox_event_cb(lv_event_t* e)
{
    lv_obj_t *target = lv_event_get_current_target(e);                 /* 获取当前触发源 */

    if(lv_msgbox_get_active_btn(target) == 2)                          /* 获取按钮索引 */
    {
    	if (UP.ES_ProTempCtrl == 0)
    	{
    		UP.ES_ProTempCtrl = 1;
    		lv_obj_add_state(btn_ctrlTempStart, LV_STATE_CHECKED);
    	}
    	else
    	{
    		UP.ES_ProTempCtrl = 0;
    		lv_obj_clear_state(btn_ctrlTempStart, LV_STATE_CHECKED);
    	}
    	send_aux(&UP.ES_ProTempCtrl);
    	lv_obj_del(lv_msgbox);
    }
    else if (lv_msgbox_get_active_btn(target) == 3)
    {
    	lv_obj_del(lv_msgbox);
    }
	if (g_RW_State == RW_READY)
	{
	    g_RW_State = RW_WRITE_NAND_SCREEN;
	}
}
/**
  * @brief	弹窗创建函数
  * @param  e 触发源
  * @retval None
  */
static void mxgbox_create(void)
{
	static const char* btns[5];
	btns[0] = " ";
	btns[1] = " ";
	btns[2] = dispText[DA.typeLanguage].ok;
	btns[3] = dispText[DA.typeLanguage].abolish;
	btns[4] = "";
	/* 消息框整体 */
	lv_msgbox = lv_msgbox_create(tabCont, dispText[DA.typeLanguage].DialogTitle, \
			dispText[DA.typeLanguage].DialogCont, btns, true);
	lv_obj_center(lv_msgbox);
	lv_obj_set_size(lv_msgbox, lv_pct(65), lv_pct(65));  //设置父对象的大小
    lv_obj_set_style_border_width(lv_msgbox, 0, LV_STATE_DEFAULT);                              /* 去除边框 */
    lv_obj_set_style_shadow_width(lv_msgbox, 20, LV_STATE_DEFAULT);                             /* 设置阴影宽度 */
    lv_obj_set_style_shadow_color(lv_msgbox, lv_color_hex(0xa9a9a9), LV_STATE_DEFAULT);         /* 设置阴影颜色 */
    lv_obj_set_style_pad_top(lv_msgbox,18,LV_STATE_DEFAULT);                                    /* 设置顶部填充 */
    lv_obj_set_style_pad_left(lv_msgbox,20,LV_STATE_DEFAULT);
    lv_obj_add_event_cb(lv_msgbox, msgbox_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
    /* 消息框标题 */
    lv_obj_t *title = lv_msgbox_get_title(lv_msgbox);                                           /* 获取标题部分 */            /* 设置字体 */
    lv_obj_set_style_text_color(title, lv_color_hex(0xff0000),LV_STATE_DEFAULT);
    /* 消息框主体 */
    lv_obj_t *content = lv_msgbox_get_content(lv_msgbox);                                       /* 获取主体部分 */         /* 设置字体 */
    lv_obj_set_style_text_color(content, lv_color_hex(0x6c6c6c), LV_STATE_DEFAULT);          /* 设置文本颜色：灰色 */
    lv_obj_set_style_pad_top(content,5,LV_STATE_DEFAULT);

    /* 消息框按钮 */
    lv_obj_t *btn = lv_msgbox_get_btns(lv_msgbox);                                           /* 获取按钮矩阵部分 */
    lv_obj_set_style_bg_opa(btn, 0, LV_PART_ITEMS);                                          /* 设置按钮背景透明度 */
    lv_obj_set_style_shadow_width(btn, 0, LV_PART_ITEMS);  				/* 去除按钮阴影 */
    lv_obj_set_style_pad_top(btn, 15, LV_STATE_DEFAULT);
    lv_obj_set_style_pad_right(btn, 5, LV_STATE_DEFAULT);
    lv_obj_set_style_text_color(btn, lv_color_hex(0x2271df),LV_PART_ITEMS);                  /* 设置文本颜色（未按下）：蓝色 */
    lv_obj_set_style_text_color(btn, lv_color_hex(0xff0000),LV_PART_ITEMS|LV_STATE_PRESSED); /* 设置文本颜色（已按下）：红色 */
}
static void list_change_event_cb(lv_event_t * e)
{
    lv_obj_t * obj = lv_event_get_target(e);
    uint16_t col;
    uint16_t row;
    lv_table_get_selected_cell(obj, &row, &col);
    for(uint16_t row_cnt = 0; row_cnt < lv_table_get_row_cnt(obj); row_cnt++)
    {
    	if(row == row_cnt)
		{
    		lv_table_add_cell_ctrl(obj, row_cnt, 0, LV_TABLE_CELL_CTRL_CUSTOM_1);
		}
		else
		{
			lv_table_clear_cell_ctrl(obj, row_cnt, 0, LV_TABLE_CELL_CTRL_CUSTOM_1);
		}
    }
    for(uint16_t row_cnt = 0; row_cnt < lv_table_get_row_cnt(obj); row_cnt++)
    {
    	for(uint16_t col_cnt = 0; col_cnt < lv_table_get_col_cnt(obj); col_cnt++)
    	{

            if (row == row_cnt && col == col_cnt)
        	{
            	lv_table_add_cell_ctrl(obj, row_cnt, col_cnt, LV_TABLE_CELL_CTRL_CUSTOM_2);
            	if (ctrl_lastRow == row && ctrl_lastCol == col)
            	{
            		if (ctrl_lastCol == 1)
            		{
                		/* 键盘参数初始化 */
                		userData[0].value = &DA.taUser_table[row].program_SP;
                		userData[0].store_enable = 0;
            			keyboard_create_float(-50, 50, &userData[0]);
            		}
            		else if (ctrl_lastCol == 2)
            		{
                		userData[1].value = &DA.taUser_table[row].program_Time_ThisStep;
                		userData[1].store_enable = 0;
            			keyboard_create_int(0, 60, &userData[1]);
            		}
            	}
        	}
        	else
        	{
        		lv_table_clear_cell_ctrl(obj, row_cnt, col_cnt, LV_TABLE_CELL_CTRL_CUSTOM_2);
        	}
    	}
    }
}
static void btn_ctrl_event_cb(lv_event_t * e)
{
	lv_event_code_t code = lv_event_get_code(e);
//	lv_obj_t* target = lv_event_get_target(e);
	if (code == LV_EVENT_CLICKED)
	{
		mxgbox_create();
	}
}
static void title_draw_event_cb(lv_event_t * e)
{
	lv_obj_draw_part_dsc_t * dsc = lv_event_get_draw_part_dsc(e);
	dsc->label_dsc->align = LV_TEXT_ALIGN_CENTER;
	dsc->label_dsc->color = lv_color_hex(0xffffff);
	dsc->rect_dsc->bg_color = lv_color_hex(0x808080);
}
static void list_draw_event_cb(lv_event_t * e)
{
    lv_obj_t * obj = lv_event_get_target(e);
    lv_obj_draw_part_dsc_t * dsc = lv_event_get_draw_part_dsc(e);

    /*If the cells are drawn...*/
    if(dsc->part == LV_PART_ITEMS) {
        uint32_t row = dsc->id /  lv_table_get_col_cnt(obj);
        uint32_t col = dsc->id - row * lv_table_get_col_cnt(obj);
        bool chk = lv_table_has_cell_ctrl(obj, row, 0, LV_TABLE_CELL_CTRL_CUSTOM_1);
		if(chk)
		{
			if(ctrl_lastRow != row)
			{
				ctrl_lastRow = row;
			}
			chk = lv_table_has_cell_ctrl(obj, row, col, LV_TABLE_CELL_CTRL_CUSTOM_2);
			if(chk)
			{
				if(ctrl_lastCol != col )
				{
					ctrl_lastCol = col;
				}
				dsc->rect_dsc->bg_color =lv_color_hex(COLOUR_WIIP_ORANGE);
			}
			else
			{
				dsc->rect_dsc->bg_color = lv_color_hex(0x1d4b78);
			}
		}
		else
		{

			dsc->rect_dsc->bg_color = lv_color_hex(0xFFFFFF);
		}

		dsc->label_dsc->align = LV_TEXT_ALIGN_CENTER;
    }
}
static void checkbox_event_cb(lv_event_t * e)
{
	lv_obj_t *target = lv_event_get_target(e); /* 获取触发源 */
	if (target == checkBox_loopEnable)
	{
		if (lv_obj_has_state(checkBox_loopEnable, LV_STATE_CHECKED))
		{
			UP.ES_Program_Cycle = 1;
		}
		else
		{
			UP.ES_Program_Cycle = 0;
		}
		send_aux(&UP.ES_Program_Cycle);
	}
}

/**
 * @brief  自定义选项卡三创建函数
 * @param  无
 * @return 无
 */
static void ctrlTempView_create(void)
{
	/* 标题栏创建 */
	list_titleInfo = lv_table_create(tabCont);
	lv_table_set_col_cnt(list_titleInfo, 3);
	lv_obj_set_style_pad_ver(list_titleInfo, 0, LV_PART_ITEMS);
	lv_obj_set_style_pad_row(list_titleInfo, 0, LV_PART_ITEMS);
	lv_obj_set_style_pad_top(list_titleInfo, 8, LV_PART_ITEMS);
	lv_obj_set_style_pad_bottom(list_titleInfo, 8, LV_PART_ITEMS);
	lv_obj_set_style_pad_left(list_titleInfo, 2, LV_PART_ITEMS);
	lv_obj_set_style_pad_right(list_titleInfo, 2, LV_PART_ITEMS);
	lv_obj_set_style_bg_color(list_titleInfo, lv_color_hex(0xFFFFFF), LV_PART_ITEMS);
	lv_obj_set_style_outline_width(list_titleInfo, 1, LV_PART_ITEMS);
	lv_obj_set_style_outline_color(list_titleInfo, lv_palette_main(LV_PALETTE_GREY), LV_PART_ITEMS);
	lv_table_set_col_width(list_titleInfo, 0, 170);
	lv_table_set_col_width(list_titleInfo, 1, 180);
	lv_table_set_col_width(list_titleInfo, 2, 180);
	lv_table_set_row_cnt(list_titleInfo, 1);
	lv_obj_set_size(list_titleInfo, lv_pct(100), lv_pct(18));
	lv_table_set_cell_value(list_titleInfo, 0, 0, dispText[DA.typeLanguage].ctrlTempID);
	lv_table_set_cell_value(list_titleInfo, 0, 1, dispText[DA.typeLanguage].stableTemp);
	lv_table_set_cell_value(list_titleInfo, 0, 2, dispText[DA.typeLanguage].stableTime);
	lv_obj_align(list_titleInfo, LV_ALIGN_TOP_MID, 0, 0);
	lv_obj_add_event_cb(list_titleInfo, title_draw_event_cb, LV_EVENT_DRAW_PART_BEGIN, NULL);

	/* 控温主体创建 */
	list_ctrlTempInfo = lv_table_create(tabCont);
	lv_table_set_col_cnt(list_ctrlTempInfo, 3);
	lv_obj_set_style_pad_ver(list_ctrlTempInfo, 0, LV_PART_ITEMS);
	lv_obj_set_style_pad_row(list_ctrlTempInfo, 0, LV_PART_ITEMS);
	lv_obj_set_style_pad_top(list_ctrlTempInfo, 8, LV_PART_ITEMS);
	lv_obj_set_style_pad_bottom(list_ctrlTempInfo, 8, LV_PART_ITEMS);
	lv_obj_set_style_pad_left(list_ctrlTempInfo, 2, LV_PART_ITEMS);
	lv_obj_set_style_pad_right(list_ctrlTempInfo, 2, LV_PART_ITEMS);
	lv_obj_set_style_bg_color(list_ctrlTempInfo, lv_color_hex(0xFFFFFF), LV_PART_ITEMS);
	lv_obj_set_style_outline_width(list_ctrlTempInfo, 1, LV_PART_ITEMS);
	lv_obj_set_style_outline_color(list_ctrlTempInfo, lv_palette_main(LV_PALETTE_GREY), LV_PART_ITEMS);
	lv_table_set_col_width(list_ctrlTempInfo, 0, 170);
	lv_table_set_col_width(list_ctrlTempInfo, 1, 180);
	lv_table_set_col_width(list_ctrlTempInfo, 2, 180);
	lv_table_set_row_cnt(list_ctrlTempInfo, 1);
	lv_obj_set_size(list_ctrlTempInfo, lv_pct(100), lv_pct(68));
	lv_obj_align(list_ctrlTempInfo, LV_ALIGN_TOP_MID, 0, lv_pct(17));
	lv_obj_add_event_cb(list_ctrlTempInfo, list_draw_event_cb, LV_EVENT_DRAW_PART_BEGIN, NULL);
	lv_obj_add_event_cb(list_ctrlTempInfo, list_change_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
	for (uint8_t i = 0; i < 8; i++)
	{
		lv_table_set_cell_value_fmt(list_ctrlTempInfo, i, 0, "%d", DA.taUser_table[i].program_Step);
		lv_table_set_cell_value_fmt(list_ctrlTempInfo, i, 1, "%.1f",DA.taUser_table[i].program_SP);
		lv_table_set_cell_value_fmt(list_ctrlTempInfo, i, 2, "%d", DA.taUser_table[i].program_Time_ThisStep);
	}

	/* 创建复选框 */
	checkBox_loopEnable = lv_checkbox_create(tabCont); /* 创建复选框 */
	lv_checkbox_set_text(checkBox_loopEnable, dispText[DA.typeLanguage].programLoop); /* 动态设置复选框的文本 */
	lv_obj_set_style_text_color(checkBox_loopEnable, lv_color_hex(0xffffff), LV_PART_MAIN);
	lv_obj_set_style_bg_color(checkBox_loopEnable, lv_color_hex(0x808080),\
			LV_PART_INDICATOR | LV_STATE_CHECKED);
	lv_obj_align(checkBox_loopEnable, LV_ALIGN_TOP_LEFT, 0,lv_pct(86));
	lv_obj_add_event_cb(checkBox_loopEnable, checkbox_event_cb,
	LV_EVENT_VALUE_CHANGED, NULL);
	/* 创建程序控温启动按钮 */
	btn_ctrlTempStart= lv_btn_create(tabCont);
    lv_obj_add_style(btn_ctrlTempStart, &style_btn4, LV_STATE_DEFAULT);
    lv_obj_align(btn_ctrlTempStart,LV_ALIGN_TOP_LEFT, lv_pct(70), lv_pct(86));
    lv_obj_t * lable = lv_label_create(btn_ctrlTempStart);
    lv_label_set_text(lable, dispText[DA.typeLanguage].start);
    lv_obj_align_to(lable, btn_ctrlTempStart, LV_ALIGN_CENTER, 0, -5);
    lv_obj_set_size(btn_ctrlTempStart, lv_pct(25), lv_pct(14));
    lv_obj_set_style_bg_color(btn_ctrlTempStart, lv_color_hex(0x18adf8), LV_STATE_CHECKED);
    lv_obj_add_event_cb(btn_ctrlTempStart, btn_ctrl_event_cb, LV_EVENT_CLICKED, NULL);
}
/**
  * @brief	侧边栏按键事件回调函数
  * @param  None
  * @retval None
  */
static void btn_tabList_cb(lv_event_t * e)
{
	lv_event_code_t code = lv_event_get_code(e);
	lv_obj_t* obj = (lv_obj_t*)lv_event_get_target(e);
	lv_obj_t* parent = lv_obj_get_parent(obj);
	if (code == LV_EVENT_CLICKED)
	{
		for (uint32_t i = 0; i < lv_obj_get_child_cnt(parent); i++)
		{
			lv_obj_t* child = lv_obj_get_child(parent, i);
			if (child == obj)
			{
				if(now_tab_ID == i)
					return;
				lv_obj_add_state(child, LV_STATE_CHECKED);
//				lv_imgbtn_set_state(child, LV_IMGBTN_STATE_PRESSED);
				lv_obj_del(tabCont);
				tabCont = lv_obj_create(parentObj);
				lv_obj_set_size(tabCont, lv_pct(73), lv_pct(90));
				lv_obj_align(tabCont, LV_ALIGN_TOP_RIGHT, -8, 24);
				lv_obj_add_style(tabCont, &style_cont1, 0);
				lv_obj_set_scrollbar_mode(tabCont, LV_SCROLLBAR_MODE_OFF); // 设置滚动条模式为永不显示
				now_tab_ID = i;
				listBtn[now_tab_ID].tab_create_func();
			}
			else
			{
				lv_obj_clear_state(child, LV_STATE_CHECKED);
//				lv_imgbtn_set_state(child, LV_IMGBTN_STATE_RELEASED);
			}
		}
	}
}
/**
  * @brief	侧边栏按键结构体初始化
  * @param  None
  * @retval None
  */
static void listBtnStr_init(void)
{
	listBtn[0].taitle = dispText[DA.typeLanguage].mode;
	listBtn[0].isEnable = 1;
	listBtn[0].tab_create_func = modeView_create;
	listBtn[1].taitle = dispText[DA.typeLanguage].program;
	listBtn[1].isEnable = 1;
	listBtn[1].tab_create_func = ctrlTempView_create;
//	listBtn[2].taitle = dispText[DA.typeLanguage].program;
//	listBtn[2].isEnable = 1;
//	listBtn[2].tab_create_func = ctrlTempView_create;
}
/**
  * @brief	共享内容创建
  * @param  None
  * @retval None
  */
static void create_commons(void)
{
	static lv_style_t tab_btn_style;	  // 侧边栏按钮样式
	static lv_style_t tab_bg_style;       // 主容器背景样式
	lv_obj_t* tabList = NULL;			  // 侧边栏容器
	lv_obj_t* tabListBtn = NULL;	/* 侧边栏按键 */
	lv_obj_t* label = NULL;			/* 侧边栏按键上标签 */
	/* 设置侧边栏按钮风格 */
	lv_style_init(&tab_btn_style);
	lv_style_set_bg_color(&tab_btn_style, lv_color_hex(0xbebdc0));
	lv_style_set_border_color(&tab_btn_style, lv_color_hex(0xbebdc0));
	lv_style_set_border_width(&tab_btn_style, 0);
	/* 设置背景风格 */
	lv_style_init(&tab_bg_style);
	lv_style_set_bg_color(&tab_bg_style, lv_color_hex(0xefefef));
	/* 背景图片对象 */
	parentObj_img = lv_img_create(lv_scr_act());
    lv_img_set_src(parentObj_img, &dsc_img_buff[2]);
    lv_img_set_offset_x(parentObj_img, -2);	 /* 图片往 X 轴偏移 */
    lv_obj_add_style(parentObj_img, &style_parentObj, LV_STATE_DEFAULT);  //添加父对象为无填充样式
    lv_obj_set_size(parentObj_img, lv_pct(100), lv_pct(100));  //设置父对象的大小
    lv_obj_align(parentObj_img, LV_ALIGN_BOTTOM_MID, 0, 0);  //设置父对象的位置
	/* 界面父对象 */
	parentObj = lv_obj_create(lv_scr_act());
	lv_obj_add_style(parentObj, &style_parentObj, LV_STATE_DEFAULT);
	lv_obj_set_size(parentObj, lv_pct(100), lv_pct(88));
	lv_obj_set_style_bg_color(parentObj,lv_palette_main(LV_PALETTE_GREEN),LV_STATE_DEFAULT);
	lv_obj_align(parentObj, LV_ALIGN_BOTTOM_LEFT, 0, 0);
	/* 添加侧边栏容器 */
	tabList = lv_obj_create(parentObj);
	lv_obj_set_size(tabList, lv_pct(27), lv_pct(90));
	lv_obj_add_style(tabList, &style_list1, 0);
	lv_obj_set_scrollbar_mode(tabList, LV_SCROLLBAR_MODE_OFF); // 设置滚动条模式为永不显示
	lv_obj_align(tabList, LV_ALIGN_TOP_LEFT, 0, 24);
	/* 添加主容器 */
	tabCont = lv_obj_create(parentObj);
	lv_obj_set_size(tabCont, lv_pct(73), lv_pct(90));
	lv_obj_add_style(tabCont, &style_cont1, 0);
	lv_obj_set_scrollbar_mode(tabCont, LV_SCROLLBAR_MODE_OFF); // 设置滚动条模式为永不显示
	lv_obj_align(tabCont, LV_ALIGN_TOP_RIGHT, -8, 24);

	/* 添加按钮到侧边栏 */
	for(uint8_t btnCnt = 0; btnCnt < LIST_BTN_QTY; btnCnt++)
	{
		if(listBtn[btnCnt].isEnable)
		{
			tabListBtn = lv_btn_create(tabList);
			lv_obj_set_size(tabListBtn, 202, 89);
			lv_obj_add_style(tabListBtn, &tab_btn_style, LV_STATE_DEFAULT);
			lv_obj_set_style_bg_color(tabListBtn, lv_color_hex(0x9c9c9c), LV_STATE_CHECKED);
			lv_obj_align(tabListBtn, LV_ALIGN_TOP_MID, 0, (btnCnt * 95));
			lv_obj_add_event_cb(tabListBtn, btn_tabList_cb, LV_EVENT_CLICKED, NULL);
			lv_obj_set_style_bg_img_src(tabListBtn, &dsc_ico_buff[3], LV_STATE_DEFAULT);
			lv_obj_set_style_bg_img_src(tabListBtn, &dsc_ico_buff[2], LV_STATE_CHECKED);
			label = lv_label_create(tabListBtn);
			lv_label_set_text(label, listBtn[btnCnt].taitle);
			lv_obj_set_style_text_color(label, lv_color_hex(0x000000), LV_STATE_DEFAULT);
			lv_obj_align(label, LV_ALIGN_CENTER, 0, 0);
		}
	}

	/* 第一个按键默认为按下状态 */
	tabListBtn = lv_obj_get_child(tabList, 0);
	lv_obj_add_state(tabListBtn, LV_STATE_CHECKED);

//	tabListBtn = lv_obj_get_child(tabList, 1);
//	lv_obj_add_state(tabListBtn, LV_STATE_DISABLED);// 将按钮设置为不可选中状态
//	lv_imgbtn_set_state(tabListBtn, LV_IMGBTN_STATE_PRESSED);
}

void UI_create_func(void)
{
	/* 初始化表格 */
	for (uint8_t i = 0; i < 8; i++)
	{
		DA.taUser_table[i].program_Step = i;
	}
	/* 侧边栏按键结构体初始化 */
	listBtnStr_init();
	/* 共享内容创建 */
	create_commons();
	/* 输入框结构体初始化 */
	textarea_init();
	/* 按钮初始化 */
	btn_init();
	/* 标签初始化 */
	label_init();     // todo:为什么放在按钮初始化之前taUser_label[1].label_cont被修改
    /* 初始化默认选项卡一 */
	now_tab_ID = 0;
	listBtn[now_tab_ID].tab_create_func();
	/* 创建一个定时器，定时刷新界面中文本框中的数据 */
	timer_refresh_func = lv_timer_create(refresh_func_cb, 500, NULL);

}


void UI_delete_func(void)
{
	if (g_RW_State == RW_READY)
	{
	    g_RW_State = RW_WRITE_NAND_SCREEN;
	}
	lv_obj_del(parentObj);
	lv_obj_del(parentObj_img);  //删除当前界面
	lv_timer_del(timer_refresh_func);
}
